package com.atlanta.adopt;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.app.AlertDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;

public class NetTask extends AsyncTask<Object,String,Document> {
	private Bitmap[] doggies;
	private Document doc;
	public Bitmap[] getDoggies(){
		return doggies;
	}
	@Override
	protected Document doInBackground(Object... arg0) {
		
		if(arg0.length<=0)
			return null;
		String URL = (String)arg0[0];//"http://sms.petpoint.com/webservices/adoptablesearch/wsAdoptableAnimals.aspx?location=HM-Adoption&stage=Available&species=Dog&sex=A&agegroup=All&onhold=A&orderby=ID&colnum=3&authkey=uj36rnuzp324mmby22csi3mbfbt4y4p1h38ahympi8f5h6vyto&css=http://www.atlantahumane.org/css/pets.css";
       long start = System.currentTimeMillis();
		try {
			doc =  Jsoup.connect(URL).get();
			Log.d("total jsoup", (System.currentTimeMillis()-start)+"");
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		//acc = doc.text()
		//Log.d("doc", doc.html());
        return doc;
		
	}
	private Bitmap downloadBitmap(String url) throws IOException {
		HttpUriRequest request = new HttpGet(url.toString());
		HttpClient httpClient = new DefaultHttpClient();
		HttpResponse response = httpClient.execute(request);

		StatusLine statusLine = response.getStatusLine();
		int statusCode = statusLine.getStatusCode();
		if (statusCode == 200) {
			HttpEntity entity = response.getEntity();
			byte[] bytes = EntityUtils.toByteArray(entity);

			Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0,
					bytes.length);
			return bitmap;
		} else {
			throw new IOException("Download failed, HTTP response code "
					+ statusCode + " - " + statusLine.getReasonPhrase());
		} // THIS IS ALL EXPERIMENTAL ^^^^^^^^^^^
	}
}
